﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.MexicoSailunMDC.Biz
 * 唯一标识：c17ca8d6-8705-4513-84ae-ae7392e00028
 * 文件名：AlarmBiz
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：llb
 * 电子邮箱：
 * 创建时间：2025/5/29 
 * 版本：V1.0.0
 * 描述：设备异常报警获取
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

namespace KingerRobot.MexicoSailunMDC.Biz;
/// <summary>
/// 异常口下胎
/// </summary>
public class AlarmBiz : K8.MDCBB.Biz
{
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 线体点位编号
        /// </summary>
        public string LineCode { get; set; }
        /// <summary>
        /// 线体点位编号
        /// </summary>
        public long LineId { get; set; }

        public long LineSign { get; set; }
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        /// <summary>
        /// 报警编号
        /// </summary>
        public bool Run { get; set; }
        /// <summary>
        /// 超时报警
        /// </summary>
        public bool TimeOver { get; set; }
        /// <summary>
        /// 电机报警	
        /// </summary>
        public bool Failure { get; set; }
        /// <summary>
        /// 急停拉绳
        /// </summary>
        public bool StopRope { get; set; }
    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {

    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }

                this._msg = $"{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        this.WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizParam
        this.BizData = writer;
        int result = 0;

        //电机运行 Run
        //超时报警 TimeOver——10100
        //电机报警 Failure——10010
        //急停拉绳 StopRope——10001

        //1、无报警：0 不需要记录
        //2、电机运行报警：11000
        //3、超时报警：10100
        //4、电机报警：10010
        //5、急停拉绳：10001


        // 设备故障
        string errorCode = string.Empty;
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        StringBuilder stringBuilder3 = new StringBuilder();
        int errorNo = 0; // 正常


        if (Data.TimeOver)
        {
            errorNo = 10100;
            errorCode = errorNo.ToString();
            stringBuilder1.Append($"[{Param.LineCode}] | 超时报警编号:{Data.TimeOver}|{SwitchData.QueryLineErrByCode(errorNo)}");
            Alarm.Instance.AddAlarm(Param.LineId, errorNo, errorCode, stringBuilder1.ToString());
            return 1;
        }


        if (Data.Failure)
        {
            errorNo = 10010;
            errorCode = errorNo.ToString();
            stringBuilder2.Append($"[{Param.LineCode}] |电机报警编号:{Data.Failure}|{SwitchData.QueryLineErrByCode(errorNo)}");
            Alarm.Instance.AddAlarm(Param.LineId, errorNo, errorCode, stringBuilder2.ToString());
            return 1;
        }
           

        if (Data.StopRope)
        {
            errorNo = 10001;
            errorCode = errorNo.ToString();
            stringBuilder3.Append($"[{Param.LineCode}] | 急停报警编号:{Data.StopRope}|{SwitchData.QueryLineErrByCode(errorNo)}");
            Alarm.Instance.AddAlarm(Param.LineId, errorNo, errorCode, stringBuilder3.ToString());
            return 1;
        }
     
        if (!Data.TimeOver && !Data.StopRope && !Data.Failure)
        {
            Alarm.Instance.AddAlarm(Param.LineId, errorNo, errorCode, string.Empty);
        }
        return result;
    }
}
